﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKRevitBase.TopologySuite;
using YJKRevitKernel.CurveUtility;
using YJKRevitKernel.RevitTopologySuite;

namespace YJKRevitKernel.FaceCalculateUtility
{
	internal static class PolygonUtility
	{
		private static bool IfTwoZIntersectionNoneZ(RevitPolygonDataBean inputOne, RevitPolygonDataBean inputTwo)
		{
			inputOne = PolygonUtility.TransformPolygonToZ(inputOne, 0.0);
			inputTwo = PolygonUtility.TransformPolygonToZ(inputTwo, 0.0);
			TopologyRelateKindEnum topologyRelateKindEnum;
			return PolygonUtility.m_useRelateOperater.TryGetPolygonRelate(inputOne, inputTwo, out topologyRelateKindEnum) && topologyRelateKindEnum != TopologyRelateKindEnum.Disjoint;
		}

		internal static List<RevitPolygonDataBean> GetIntersectionPolygonNoneZ(RevitPolygonDataBean inputOne, RevitPolygonDataBean inputTwo)
		{
			return PolygonUtility.GetWantPolygonNoneZ(inputOne, inputTwo, BooleanOperatorKindEnum.Intersection);
		}

		internal static List<RevitPolygonDataBean> GetDifferencePolygonNoneZ(RevitPolygonDataBean inputOne, RevitPolygonDataBean inputTwo)
		{
			return PolygonUtility.GetWantPolygonNoneZ(inputOne, inputTwo, BooleanOperatorKindEnum.Difference);
		}

		private static List<RevitPolygonDataBean> GetWantPolygonNoneZ(RevitPolygonDataBean inputOne, RevitPolygonDataBean inputTwo, BooleanOperatorKindEnum wantKind)
		{
			List<RevitPolygonDataBean> list = new List<RevitPolygonDataBean>();
			if (!PolygonUtility.IfTwoZIntersectionNoneZ(inputOne, inputTwo))
			{
				return list;
			}
			inputOne = PolygonUtility.TransformPolygonToZ(inputOne, 0.0);
			inputTwo = PolygonUtility.TransformPolygonToZ(inputTwo, 0.0);
			if (!PolygonUtility.m_useBooleanOperater.TryGetBooleanResult(inputOne, inputTwo, wantKind, out list))
			{
				list = ((list == null) ? new List<RevitPolygonDataBean>() : list);
			}
			return list;
		}

		internal static RevitPolygonDataBean TransformPolygonToZ(RevitPolygonDataBean input, double inputZ)
		{
			List<Curve> list = new List<Curve>();
			foreach (Curve inputCurve in input.ShellCurveLoop)
			{
				list.Add(CurveUtilityMethod.TransformCurveAtZAixs(inputCurve, inputZ, true));
			}
			CurveLoop inputShell = CurveLoop.Create(list);
			List<CurveLoop> list2 = new List<CurveLoop>();
			if (input.LstlstHolesCurves != null)
			{
				foreach (List<Curve> list3 in input.LstlstHolesCurves)
				{
					List<Curve> list4 = new List<Curve>();
					foreach (Curve inputCurve2 in list3)
					{
						list4.Add(CurveUtilityMethod.TransformCurveAtZAixs(inputCurve2, inputZ, true));
					}
					CurveLoop item = CurveLoop.Create(list4);
					list2.Add(item);
				}
			}
			return new RevitPolygonDataBean(inputShell, list2, 3);
		}

		private static RevitPolygonRelateOperater m_useRelateOperater = new RevitPolygonRelateOperater();

		private static RevitPolygonBooleanOperater m_useBooleanOperater = new RevitPolygonBooleanOperater();

		private const double m_UseZ = 0.0;
	}
}
